package base.src;

import game.base.BaseButton;
import game.base.BaseCanvas;
import game.base.UIButtonClick;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.view.MotionEvent;
import android.view.View;
import base.game.BaseShip;
import base.game.Map;
import base.game.Player;
import base.game.Shot;

import communication.bluethooth.BluethoothActivity;

public class GameScreen extends BaseCanvas
{
	enum gameState {
		WAITING, DEPLOYING, TURN_MY, TURN_THEIR, END
	}
	
	GameConnection gameConnection;
	gameState state = gameState.DEPLOYING;
	Paint defaultPaint;
	Player player1, player2;
	Map map, enemyMap;
	boolean viewMyMap = true;
	boolean myTurn = BluethoothActivity.server;
	int touchX = 0, touchY = 0;
	BaseShip selectedShip;
	private BaseButton buttonStart = new BaseButton(new Point(5, 15), Images.buttonStart, Images.buttonStartOn);
	public GameScreen(BatalhaNavalActivity _activity)
	{
		super(_activity);
		player1 = new Player();
		player2 = new Player();
		defaultPaint = new Paint();
		Map.setStartPositionX(Properties.getInterfaceArea().width());
		Map.calculateRowSize(Properties.getScreenArea().width() - Properties.getInterfaceArea().width(), Properties.getScreenArea()
				.height());
		map = new Map(player1);
		enemyMap = new Map(player2);
		player1.startShips(map);
		player2.startShips(map);
		gameConnection = new GameConnection(this);
		buttonStart.setActionClick(new UIButtonClick() {
			
			@Override
			public void performActionClick(MotionEvent event) {
				gameConnection.sendPlayer(player1);
				Player enemy = gameConnection.readPlayer();
				player2 = enemy;
				if (myTurn) {
					state = gameState.TURN_MY;
				} else {
					state = gameState.TURN_THEIR;
				}
			}
		});

	}
	

	@Override
	public void onDraw(Canvas canvas)
	{
		canvas.drawBitmap(Images.sea.getImage(), Images.sea.getArea(), Properties.getScreenArea(), new Paint());
		if (viewMyMap) {
			map.onDraw(canvas, player1);
		} else {
			enemyMap.onDraw(canvas, player1);
		}
		
		if (state == gameState.DEPLOYING) {
			Paint pAlpha = new Paint();
			pAlpha.setAlpha(100);
			pAlpha.setFilterBitmap(true);
			player1.drawShips(canvas, pAlpha);
			pAlpha.setAlpha(255);
			if (selectedShip != null) {
				selectedShip.onDraw(canvas, pAlpha);
			}
		}
		canvas.drawBitmap(Images.interfaceBackground.getImage(), Images.interfaceBackground.getArea(), Properties.getInterfaceArea(),
				new Paint());
		if (buttonStart != null) {
			buttonStart.draw(canvas);
		}
		if (state == gameState.TURN_MY) {
			canvas.drawText("Shot!", 15, 15, defaultPaint);
		}
	}

	@Override
	public void onUpdate(long difftime)
	{
		if (buttonStart != null) {
			buttonStart.update(difftime);
		}
		if(state==gameState.TURN_THEIR){
			Shot shot = gameConnection.readShot();
			if (shot != null) {
				map.shot(shot.x, shot.y);
				state = gameState.TURN_MY;
			}
		}
	}

	@Override
	public void onSensorChanged(SensorEvent event)
	{
		
		
	}

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		if (buttonStart != null) {
		if (buttonStart.eventHandler(event)) {
			return false;
		}
		}
		switch (state) {
		case DEPLOYING:
			touchX = (int) event.getX();
			touchY = (int) event.getY();
			if (selectedShip == null) {
				selectedShip = player1.selectShip((touchX - Map.mapStartX) / Map.rowWidth, (touchY - Map.mapStartY) / Map.rowHeight);
			} else {
				BaseShip tmp = player1.selectShip((touchX - Map.mapStartX) / Map.rowWidth, (touchY - Map.mapStartY) / Map.rowHeight);
				if (tmp != null && tmp != selectedShip) {
					selectedShip = tmp;
				} else {
				selectedShip.move(map, (touchX - Map.mapStartX) / Map.rowWidth, (touchY - Map.mapStartY) / Map.rowHeight);
				}
			}
			break;
		case TURN_MY:
			touchX = (int) event.getX();
			touchY = (int) event.getY();
			enemyMap.shot(touchX, touchY);
			Shot shot = new Shot();
			gameConnection.sendShot(shot);
			state = gameState.TURN_THEIR;
			break;
		}
		return false;
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy)
	{
	}

	@Override
	public boolean onTouch(View v, MotionEvent event)
	{
		return false;
	}


	public void startDeploying() {
		state = gameState.DEPLOYING;
	}

}
